using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.Page
{
	public class SnsMessageListPage : BasePage
	{
		private const string TAG = "SnsMessageListPage";
		private const int fetchPerTime = 10;
		public int initMsgNum;
		private int nowIndex;
		public ObservableCollection<SnsMsg> itemSource;
		private bool bIsFirstLoad = true;
		private ScrollViewer scrollViewer;
		private EventWatcher mWatchSnsInfo;
		private ApplicationBar mBar;
		private List<ulong> hasDelObjIdList;
		internal Grid LayoutRoot;
		internal Grid MassSendTitle;
		internal TextBlock PageTitle;
		internal ListBox SnsMsgList;
		internal Grid LoadingView;
		internal PerformanceProgressBar NetStatusProgressBar;
		internal Grid NoMemberView;
		private bool _contentLoaded;
		public SnsMessageListPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.SnsMessageListPage_Loaded));
		}
		private void SnsMessageListPage_Loaded(object sender, RoutedEventArgs e)
		{
			if (this.bIsFirstLoad)
			{
				base.SetValue(TiltEffect.IsTiltEnabledProperty, true);
				this.itemSource = new ObservableCollection<SnsMsg>();
				this.SnsMsgList.set_ItemsSource(this.itemSource);
				List<SnsMsg> listOrderByTime;
				if (this.initMsgNum == 0)
				{
					listOrderByTime = StorageMgr.snsMsg.getListOrderByTime(0, 10);
				}
				else
				{
					listOrderByTime = StorageMgr.snsMsg.getListOrderByTime(0, this.initMsgNum);
				}
				if (listOrderByTime == null || listOrderByTime.get_Count() == 0)
				{
					this.ChangeToNoMsgView();
					return;
				}
				using (List<SnsMsg>.Enumerator enumerator = listOrderByTime.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsMsg current = enumerator.get_Current();
						this.itemSource.Add(current);
					}
				}
				SnsMsg snsMsg = new SnsMsg();
				if (this.initMsgNum == 0)
				{
					if (StorageMgr.snsMsg.count() != listOrderByTime.get_Count())
					{
						snsMsg.strObjectID = "ToLoadMode";
						this.itemSource.Add(snsMsg);
						this.nowIndex = this.itemSource.get_Count() - 1;
					}
					else
					{
						this.nowIndex = this.itemSource.get_Count();
					}
				}
				else
				{
					snsMsg.strObjectID = "ClickMode";
					this.itemSource.Add(snsMsg);
					this.nowIndex = this.itemSource.get_Count() - 1;
				}
				this.registerEvent();
				SnsMsgMgr.setSnsMsgUnreadCount(0);
				this.ChangeToMsgView();
				this.bIsFirstLoad = false;
			}
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.GetScrollviewerEvent();
			});
		}
		private void GetScrollviewerEvent()
		{
			if (this.scrollViewer == null)
			{
				this.scrollViewer = (ScrollViewer)UIElementTraveler.FindElementRecursive(this.SnsMsgList, typeof(ScrollViewer));
				if (this.scrollViewer != null)
				{
					FrameworkElement frameworkElement = VisualTreeHelper.GetChild(this.scrollViewer, 0) as FrameworkElement;
					if (frameworkElement != null)
					{
						VisualStateGroup visualStateGroup = UIElementTraveler.FindVisualState(frameworkElement, "VerticalCompression");
						if (visualStateGroup != null)
						{
							visualStateGroup.add_CurrentStateChanging(new EventHandler<VisualStateChangedEventArgs>(this.vgroup_CurrentStateChanging));
						}
					}
				}
			}
		}
		private void vgroup_CurrentStateChanging(object sender, VisualStateChangedEventArgs e)
		{
			e.get_NewState().get_Name() == "CompressionTop";
			if (e.get_NewState().get_Name() == "CompressionBottom" && this.initMsgNum == 0)
			{
				this.LoadMore();
			}
			e.get_NewState().get_Name() == "NoVerticalCompression";
		}
		public void LoadMore()
		{
			if (this.itemSource == null || this.itemSource.get_Count() == 0)
			{
				return;
			}
			SnsMsg snsMsg = null;
			if (this.itemSource.get_Item(this.itemSource.get_Count() - 1).strObjectID != "ToLoadMode")
			{
				return;
			}
			snsMsg = this.itemSource.get_Item(this.itemSource.get_Count() - 1);
			snsMsg.strObjectID = "LoadingMode";
			this.itemSource.set_Item(this.itemSource.get_Count() - 1, snsMsg);
			List<SnsMsg> listOrderByTime = StorageMgr.snsMsg.getListOrderByTime(this.nowIndex, 10);
			if (listOrderByTime == null || listOrderByTime.get_Count() == 0)
			{
				if (this.itemSource.get_Item(this.itemSource.get_Count() - 1).strObjectID == "LoadingMode")
				{
					this.itemSource.RemoveAt(this.itemSource.get_Count() - 1);
				}
				return;
			}
			using (List<SnsMsg>.Enumerator enumerator = listOrderByTime.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsMsg current = enumerator.get_Current();
					this.itemSource.Insert(this.itemSource.get_Count() - 1, current);
				}
			}
			this.nowIndex = this.itemSource.get_Count() - 1;
			if (this.nowIndex != StorageMgr.snsMsg.count())
			{
				snsMsg.strObjectID = "ToLoadMode";
				this.itemSource.set_Item(this.itemSource.get_Count() - 1, snsMsg);
				return;
			}
			this.itemSource.RemoveAt(this.itemSource.get_Count() - 1);
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("SnsMessageListPage", "OnPageIn");
			if (object_0 != null && Enumerable.Count<object>(object_0) == 1)
			{
				this.initMsgNum = (int)object_0[0];
			}
			this.ChangeToLoadingView();
		}
		protected override void OnPageOut()
		{
			this.unRegisterEvent();
			SnsMsgMgr.setSnsMsgUnreadCount(0);
			this.ClearUselessMsg();
			if (this.hasDelObjIdList != null)
			{
				this.hasDelObjIdList.Clear();
			}
			Log.smethod_5("SnsMessageListPage", "OnPageOut");
		}
		private void registerEvent()
		{
			if (this.mWatchSnsInfo == null)
			{
				this.mWatchSnsInfo = new EventWatcher(this, this, new EventHandlerDelegate(this.OnSnsMsgStorageEvent));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSMSG_ADD, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSMSG_DEL, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSMSG_MODIFY, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSINFO_ADD, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SNS_GETDETAIL_ERR, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatchSnsInfo);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchSnsInfo != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSMSG_ADD, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSMSG_DEL, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSMSG_MODIFY, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSINFO_ADD, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_SNS_GETDETAIL_ERR, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatchSnsInfo);
				this.mWatchSnsInfo = null;
			}
		}
		private void OnSnsMsgStorageEvent(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			List<SnsMsg> listObject = evtArgs.getListObject<SnsMsg>();
			EventConst mEventID = evtArgs.mEventID;
			if (mEventID > EventConst.ON_STORAGE_SNSINFO_ADD)
			{
				switch (mEventID)
				{
				case EventConst.ON_STORAGE_SNSMSG_ADD:
					this.AddNewMsg(listObject);
					return;
				case EventConst.ON_STORAGE_SNSMSG_DEL:
					this.DelOldMsg(listObject);
					return;
				case EventConst.ON_STORAGE_SNSMSG_MODIFY:
					break;
				default:
				{
					if (mEventID != EventConst.ON_MGR_HEAD_IMG_READY)
					{
						return;
					}
					List<string> listObject2 = evtArgs.getListObject<string>();
					if (listObject2 != null)
					{
						CommentMsgCtrl.RefreshImage(listObject2);
					}
					break;
				}
				}
				return;
			}
			if (mEventID == EventConst.ON_NETSCENE_SNS_GETDETAIL_ERR)
			{
				ulong strObjId = (ulong)evtArgs.mObject;
				this.AddItemToList(strObjId);
				return;
			}
			if (mEventID != EventConst.ON_STORAGE_SNSINFO_ADD)
			{
				return;
			}
			List<SnsInfo> listObject3 = evtArgs.getListObject<SnsInfo>();
			this.RefreshCommentItemUI(listObject3);
		}
		private void AddNewMsg(List<SnsMsg> listInfos)
		{
			if (this.itemSource == null || this.itemSource.get_Count() == 0)
			{
				this.ChangeToMsgView();
			}
			for (int i = listInfos.get_Count() - 1; i >= 0; i--)
			{
				this.itemSource.Insert(0, listInfos.get_Item(i));
			}
			this.nowIndex += listInfos.get_Count();
		}
		private void DelOldMsg(List<SnsMsg> listInfos)
		{
			using (List<SnsMsg>.Enumerator enumerator = listInfos.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsMsg current = enumerator.get_Current();
					using (IEnumerator<SnsMsg> enumerator2 = this.itemSource.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							SnsMsg current2 = enumerator2.get_Current();
							if (current.nMsgID == current2.nMsgID)
							{
								this.itemSource.Remove(current2);
								break;
							}
						}
					}
				}
			}
			this.nowIndex -= listInfos.get_Count();
			if (this.nowIndex == 0)
			{
				this.itemSource.Clear();
				this.ChangeToNoMsgView();
			}
		}
		private void ModifyOldMsg(List<SnsMsg> listInfos)
		{
			using (List<SnsMsg>.Enumerator enumerator = listInfos.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsMsg current = enumerator.get_Current();
					using (IEnumerator<SnsMsg> enumerator2 = this.itemSource.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							SnsMsg current2 = enumerator2.get_Current();
							if (current.nMsgID == current2.nMsgID)
							{
								break;
							}
						}
					}
				}
			}
		}
		private ApplicationBar GetAppBar()
		{
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			this.mBar.set_Mode(1);
			if (this.mBar != null)
			{
				this.mBar.set_BackgroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_BK_BRUSH).get_Color());
				this.mBar.set_ForegroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_FG_BRUSH).get_Color());
			}
			ApplicationBarMenuItem applicationBarMenuItem = new ApplicationBarMenuItem(strings.WC_COMMENT_LIST_CLEAR_ALL);
			applicationBarMenuItem.add_Click(new EventHandler(this.ClearButton_Click));
			this.mBar.get_MenuItems().Add(applicationBarMenuItem);
			return this.mBar;
		}
		private void ClearButton_Click(object sender, EventArgs e)
		{
			MMMessageBox mMMessageBox = MMMessageBox.Show(strings.WC_COMMENT_LIST_CLEAR_ALL, null, true, strings.COM_Confirm, strings.COM_Cancel);
			mMMessageBox.ReturnEvent = new DlgReturnEvent(this.OnClearMsgHandler);
		}
		private void OnClearMsgHandler(DlgReturnEventArg dlgReturnEventArg_0)
		{
			switch (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0))
			{
			case 0:
				StorageMgr.snsMsg.clearAll();
				this.ChangeToNoMsgView();
				break;
			case 1:
				break;
			default:
				return;
			}
		}
		private void ChangeToNoMsgView()
		{
			this.SnsMsgList.set_Visibility(1);
			this.NoMemberView.set_Visibility(0);
			this.LoadingView.set_Visibility(1);
			base.set_ApplicationBar(null);
		}
		private void ChangeToMsgView()
		{
			this.SnsMsgList.set_Visibility(0);
			this.NoMemberView.set_Visibility(1);
			this.LoadingView.set_Visibility(1);
			if (base.get_ApplicationBar() == null)
			{
				if (this.mBar == null)
				{
					base.set_ApplicationBar(this.GetAppBar());
					return;
				}
				base.set_ApplicationBar(this.mBar);
			}
		}
		private void ChangeToLoadingView()
		{
			this.SnsMsgList.set_Visibility(1);
			this.NoMemberView.set_Visibility(1);
			this.LoadingView.set_Visibility(0);
			base.set_ApplicationBar(null);
		}
		private void SnsMsg_Click(object sender, object object_0)
		{
		}
		private void RefreshCommentItemUI(List<SnsInfo> listInfos)
		{
			if (listInfos != null && listInfos.get_Count() != 0)
			{
				for (int i = 0; i < listInfos.get_Count(); i++)
				{
					CommentMsgCtrl.RefreshUI(listInfos.get_Item(i).strObjectID);
				}
				return;
			}
		}
		private void AddItemToList(ulong strObjId)
		{
			if (this.hasDelObjIdList == null)
			{
				this.hasDelObjIdList = new List<ulong>();
			}
			if (!this.hasDelObjIdList.Contains(strObjId))
			{
				this.hasDelObjIdList.Add(strObjId);
			}
		}
		private void ClearUselessMsg()
		{
			if (this.hasDelObjIdList != null && this.hasDelObjIdList.get_Count() != 0)
			{
				for (int i = 0; i < this.hasDelObjIdList.get_Count(); i++)
				{
					SnsMsgMgr.deleteMsg(this.hasDelObjIdList.get_Item(i));
				}
				return;
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/SnsMsg/SnsMessageListPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.MassSendTitle = (Grid)base.FindName("MassSendTitle");
			this.PageTitle = (TextBlock)base.FindName("PageTitle");
			this.SnsMsgList = (ListBox)base.FindName("SnsMsgList");
			this.LoadingView = (Grid)base.FindName("LoadingView");
			this.NetStatusProgressBar = (PerformanceProgressBar)base.FindName("NetStatusProgressBar");
			this.NoMemberView = (Grid)base.FindName("NoMemberView");
		}
	}
}
